<?php
class CommonFunc {
	public static function subAddress($addr, $num = 48, $suffix = '') {
		$len = strlen ( $addr );
		$suffix = '';
		
		if ($len > $num) {
			$len = $num;
			for($idx = 0; $idx < $len;) {
				if (ord ( $addr [$idx] ) > 0x7f) {
					$idx += 3;
				} else {
					$idx ++;
				}
			}
			return substr ( $addr, 0, $idx ) . $suffix;
		} else
			return $addr;
	
	}
	
	public static function formatUnixtime($timestamp, $type) {
		
		switch ($type) {
			case 1 :
				return date ( 'Y-m-d H:i:s', $timestamp );
				break;
			case 2 :
				return date ( 'm-d', $timestamp );
				break;
			case 3 :
				return date ( 'm/d', $timestamp );
				break;
		}
	}
	
	public static function Html2Text($str, $r = 0) {
		if (! function_exists ( 'SpHtml2Text' )) {
			function SpHtml2Text($str) {
				$str = preg_replace ( "/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU", "", $str );
				$alltext = "";
				$start = 1;
				for($i = 0; $i < strlen ( $str ); $i ++) {
					if ($start == 0 && $str [$i] == ">") {
						$start = 1;
					} else if ($start == 1) {
						if ($str [$i] == "<") {
							$start = 0;
							$alltext .= " ";
						} else if (ord ( $str [$i] ) > 31) {
							$alltext .= $str [$i];
						}
					}
				}
				$alltext = str_replace ( "　", " ", $alltext );
				$alltext = preg_replace ( "/&([^;&]*)(;|&)/", "", $alltext );
				$alltext = preg_replace ( "/[ ]+/s", " ", $alltext );
				return $alltext;
			}
		}
		if ($r == 0) {
			return SpHtml2Text ( $str );
		} else {
			$str = SpHtml2Text ( stripslashes ( $str ) );
			return addslashes ( $str );
		}
	}
	
	public static function YiiobjectToArray($obj, $type = null, $with = null) {
		if (! $type) {
			return FALSE;
		}
		if ($type == "one")
			return $obj->attributes;
		if ($type == "all") {
			$result = array ();
			$i = 0;
			foreach ( $obj as $model ) {
				$result [$i] = $model->attributes;
				if ($with) {
					$result [$i] ['meminfo'] = $model->$with->attributes;
				}
				$i ++;
			}
			return $result;
		}
		return FALSE;
	}
	public static function AjaxCallbackMessage($msg = '',$code = '200', $callbackType = 'forward', $forwardUrl = '') {
		$array = array (
			
			"statusCode" => $code, 
			"message" => Yii::app()->params['success'], 
			"callbackType" => $callbackType, 
			"forwardUrl" => $forwardUrl 
		);
		return json_encode ( $array );
	}
	public static function input() {
		if (! (isset ( $_GET ) || isset ( $_POST ))) {
			return FALSE;
		}
		$_GET = self::daddslashes ( $_GET );
		$_POST = self::daddslashes ( $_POST );
		if ($_SERVER ['REQUEST_METHOD'] == 'POST' && ! empty ( $_POST )) {
			$_GET = array_merge ( $_GET, $_POST );
		}
		return $_GET;
	}
	public static function daddslashes($string, $force = 1) {
		if (is_array ( $string )) {
			$keys = array_keys ( $string );
			foreach ( $keys as $key ) {
				$val = $string [$key];
				unset ( $string [$key] );
				$string [addslashes ( $key )] = self::daddslashes ( $val, $force );
			}
		} else {
			$string = addslashes ( $string );
		}
		return $string;
	}
	
	
	public static function yiiPage($modelFrom,$model='',$pagesiz='',$showpage='',$itemview='',$criteria=''){
		$criteria = new CDbCriteria ( );
		$dataProvider = new CActiveDataProvider ( $model, array (
			
			'pagination' => array (
				
				'pageSize' => $pagesiz?$pagesiz:Yii::app ()->params ['postsPerPage'] 
			), 
			'criteria' =>$criteria
		) );
		$pages = $modelFrom->render ( $showpage, array (
			
			"dataProvider" => $dataProvider, 
			"itemView" =>  $itemview
		), true );
		return $pages;
	}
}